package com.jxpanda.infrastructure.crypto.toolkit;

import com.jxpanda.infrastructure.crypto.constant.AlgorithmEncrypt;

import javax.crypto.SecretKey;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

public class KeyLoadKit {

    /**
     * 从原始字节数组加载密钥（无需 Base64）
     */
    public static SecretKey loadSecretKey(AlgorithmEncrypt.Symmetric symmetricAlgorithm, byte[] keyBytes) {
        return symmetricAlgorithm.keyProvider()
                .loadSecretKey(keyBytes);
    }

    /**
     * 从 Base64 字符串加载对称密钥
     */
    public static SecretKey loadSecretKey(AlgorithmEncrypt.Symmetric symmetricAlgorithm, String keyString) {
        return symmetricAlgorithm.keyProvider()
                .loadSecretKey(keyString);
    }

    /**
     * 从 Base64 字符串加载对称密钥
     */
    public static SecretKey loadSecretKeyFromBase64(AlgorithmEncrypt.Symmetric symmetricAlgorithm, String base64Key) {
        return symmetricAlgorithm.keyProvider()
                .loadSecretKeyFromBase64(base64Key);
    }


    /**
     * 从文件加载密钥，假设文件内容为 Base64 字符串
     */
    public static SecretKey loadKeyFromFile(AlgorithmEncrypt.Symmetric symmetricAlgorithm, String filePath) {
        return symmetricAlgorithm.keyProvider()
                .loadSecretKeyFromFile(filePath);
    }

    public static PublicKey loadPublicKey(AlgorithmEncrypt.Asymmetric asymmetricAlgorithm, byte[] bytes) {
        return asymmetricAlgorithm.keyPairProvider()
                .loadPublicKey(bytes);
    }

    /**
     * 从 Base64 或 PEM 格式字符串加载公钥
     */
    public static PublicKey loadPublicKey(AlgorithmEncrypt.Asymmetric asymmetricAlgorithm, String publicKeyString) {
        return asymmetricAlgorithm.keyPairProvider()
                .loadPublicKey(publicKeyString);
    }

    /**
     * 从文件加载公钥（文件内容为 PEM/DER 的 Base64 编码）
     */
    public static PublicKey loadPublicKeyFromFile(AlgorithmEncrypt.Asymmetric asymmetricAlgorithm, String path) {
        return asymmetricAlgorithm.keyPairProvider()
                .loadPublicKeyFromFile(path);
    }


    public static PrivateKey loadPrivateKey(AlgorithmEncrypt.Asymmetric asymmetricAlgorithm, byte[] bytes) {
        return asymmetricAlgorithm.keyPairProvider()
                .loadPrivateKey(bytes);
    }

    /**
     * 从 Base64 或 PEM 格式字符串加载私钥
     */
    public static PrivateKey loadPrivateKey(AlgorithmEncrypt.Asymmetric asymmetricAlgorithm, String privateKeyString) {
        return asymmetricAlgorithm.keyPairProvider()
                .loadPrivateKey(privateKeyString);
    }

    /**
     * 从文件加载私钥（文件内容为 PEM/DER 的 Base64 编码）
     */
    public static PrivateKey loadPrivateKeyFromFile(AlgorithmEncrypt.Asymmetric asymmetricAlgorithm, String path) {
        return asymmetricAlgorithm.keyPairProvider()
                .loadPrivateKeyFromFile(path);
    }

    /**
     * 从字符串加载一对密钥
     */
    public static KeyPair loadKeyPair(AlgorithmEncrypt.Asymmetric asymmetricAlgorithm, String publicKeyString, String privateKeyString) {
        return asymmetricAlgorithm.keyPairProvider()
                .loadKeyPair(publicKeyString, privateKeyString);
    }

    /**
     * 从文件加载一对密钥
     */
    public static KeyPair loadKeyPairFromFiles(AlgorithmEncrypt.Asymmetric asymmetricAlgorithm, String publicKeyPath, String privateKeyPath) {
        return asymmetricAlgorithm.keyPairProvider()
                .loadKeyPairFromFiles(publicKeyPath, privateKeyPath);
    }

}
